package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐浏览接口")
@Slf4j
public class SetmealController {
    private final SetmealService setmealService;

    @Autowired
    public SetmealController(SetmealService setmealService) {
        this.setmealService = setmealService;
    }

    /**
     * 根据套餐实体DTO 新增套餐
     *
     * @param setmealDTO 套餐DTO
     * @return Result
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CachePut(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result<?> saveSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("执行 新增套餐 接口 : {}",setmealDTO);
        setmealService.saveSetmealWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id集合删除套餐
     *
     * @param setmealIds 套餐id集合
     * @return Result
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = {"setmealCache", "setmealWithDishCache"},allEntries = true)
    public Result<?> deleteSetmeal(@RequestParam("ids") List<Long> setmealIds) {
        log.info("执行 批量删除 接口 : {}",setmealIds);
        setmealService.deleteSetmealBatch(setmealIds);
        return Result.success();
    }


    /**
     * 修改套餐
     *
     * @param setmealDTO 套餐DTO
     * @return Result
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = {"setmealCache", "setmealWithDishCache"},allEntries = true)
    public Result<?> updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("执行 修改套餐 接口 :{}",setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }


    /**
     * 套餐起售停售
     *
     * @param status 套餐状态
     * @param setmealId 套餐id
     * @return Result
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐启售停售")
    @CacheEvict(cacheNames = {"setmealCache", "setmealWithDishCache"},allEntries = true)
    public Result<?> startOrStopSetmeal(@PathVariable Integer status, @RequestParam("id") Long setmealId) {
        log.info("执行 套餐起售停售 接口: id:{} status: {}",setmealId,status);
        setmealService.startOrStopSetmeal(status, setmealId);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO 套餐DTO
     * @return Result
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> pageQuerySetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("执行 分页查询套餐 接口 :{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuerySetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐，用于修改页面回显数据
     *
     * @param setmealId 套餐id
     * @return 套餐实体类VO
     */
    @GetMapping("/{setmealId}")
    @ApiOperation("根据套餐id查询套餐")
    public Result<SetmealVO> getSetmealById(@PathVariable Long setmealId) {
        log.info("执行 根据套餐id查询套餐 接口 :{}",setmealId);
        SetmealVO setmealVO = setmealService.getSetmealWithDishBySetmealId(setmealId);
        return Result.success(setmealVO);
    }


}
